/*
 * main.cpp
 *
 *  Created on: Mar 18, 2012
 *      Author: Victor Nidens victornlj@gmail.com
 * 		Author:	Mikhail S. Ananyevskiy, PhD, msaipme@yandex.ru
 *
 *	This file is part of Cloud Mechatronic Laboratory (CMLab) software.
 *
 *	Cloud Mechatronic Laboratory (CMLab) for CMLab is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Cloud Mechatronic Laboratory (CMLab) software is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Cloud Mechatronic Laboratory (CMLab). If not, see <http://www.gnu.org/licenses/>.
 */

#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
#include "ServerDaemon.h"

using namespace std;


/**
 * Parse server config file ('server.conf')
 *
 * @return	0		- configuration loaded successfully
 * 		-1		- file can't be opened
 * 		-2		- some of the output parameters are NULL
 *
 *
 */
int initConfig(ServerConfig *config){
	ifstream configFile;
	string line;

	configFile.open("server.conf", ios::in);

	if(!configFile.is_open()){
		cerr << "Can't open configuration file" << endl;
		return -1;
	}

	if(!config){
		cerr << "Can't parse configuration file" << endl;
		return -2;
	}

	while(!configFile.eof()){
		getline(configFile, line);

		if(line.empty()){
			continue;
		}

		if(line.find("server.name") != string::npos){

			config->name = line.substr(line.find('=') + 1);

			if(config->name.length() > 16){
				cerr << "[WARNING] >> serv.name too long." << endl << "It will be truncated up to 16 characters." << endl;
				config->name.resize(16);
				continue;
			}

		}

		if(line.find("server.udp_port") != string::npos){
			line = line.substr(line.find('=') + 1);
			config->udpPort = atoi(line.c_str());
		}

		if(line.find("server.tcp_port") != string::npos){
			line = line.substr(line.find('=') + 1);
			config->tcpPort = atoi(line.c_str());
		}

		if(line.find("server.regulator_port") != string::npos){
			line = line.substr(line.find('=') + 1);
			config->regulatorPort = atoi(line.c_str());
		}

		if(line.find("server.ip") != string::npos){
			line = line.substr(line.find('=') + 1);

			line.append(".\n");

			uint8 ip[4] = {0};

			for(int i = 0; i < 4; i++){
				ip[i] = atoi(line.substr(0, line.find('.')).c_str());
				line = line.substr(line.find('.') + 1);
			}

			memcpy(&config->ip, ip, sizeof(ip));

		}

		if(line.find("server.save_packet_statistics") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->savePacketStat = atoi(line.c_str()) != 0;

		}

		if(line.find("server.db_host") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbHost = line;
		}

		if(line.find("server.db_port") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbPort = atoi(line.c_str());
		}

		if(line.find("server.db_user") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbUser = line;
		}

		if(line.find("server.db_password") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbUserPassword = line;
		}

		if(line.find("server.db_name") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbName = line;
		}

		if(line.find("server.db_table") != string::npos){
			line = line.substr(line.find('=') + 1);

			config->dbTable = line;
		}

	}

	configFile.close();

	return 0;
}


int main(int argc, char **argv){

	ServerConfig config;

	initConfig(&config);

	ServerDaemon server(config);

	server.exec();

	return 0;

}


